package com.carkeeper.order.service.impl;

import com.carkeeper.entity.Result;
import com.carkeeper.entity.StatusCode;
import com.carkeeper.order.dao.BillMapper;
import com.carkeeper.order.pojo.Bill;
import com.carkeeper.order.service.BillService;
import com.carkeeper.shop.feign.StoreFeign;
import com.carkeeper.shop.pojo.Store;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BillServiceImpl implements BillService {

    @Autowired
    private BillMapper billMapper;

    @Autowired
    private StoreFeign storeFeign;

    /**
     * 服务商账单分页查询
     *
     * @param searchMap 条件查询
     * @param page      当前页码
     * @param pageSize  每页显示条数
     * @return
     */
    @Override
    public Page<Bill> billPage(Map<String, Object> searchMap, int page, int pageSize) {
        PageHelper.startPage(page, pageSize);
        Example example = createExample(searchMap);
        return (Page<Bill>) billMapper.selectByExample(example);
    }

    /**
     * 根据账单编号查询账单详情
     *
     * @param code
     * @return
     */
    @Override
    public Result selectBillByCode(String code) {
        //创建map集合封装数据
        Map<String, Object> map = new HashMap();
        Bill bill1 = new Bill();
        bill1.setCode(code);
        Bill bill = billMapper.selectOne(bill1);
        //调用feign接口查询商家信息
        Store store = storeFeign.findStoreById(bill.getStoreId() + "").getData();
        System.out.println(store);
        //封装数据
        map.put("createTime", bill.getCreateTime()); //账单生成时间
        map.put("code", bill.getCode());  //账单编号
        map.put("storeName", bill.getStoreName());  //商家名称
        map.put("storeCode", bill.getStoreId());  //商家编码
        map.put("storeAddress", store.getAddress());  //地址
        map.put("contactName", store.getContactName());  //联系人
        map.put("contactPhone", store.getContactPhone());  //联系电话
        map.put("price", bill.getPrice()); //账单金额
        map.put("summary", bill.getSummary()); //账单摘要

        return new Result(true, StatusCode.OK, "查询成功", map);
    }

    /**
     * 查询所有未支付的账单
     *
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Page<Bill> serviceBill(Map<String,Object> searchMap, int page, int pageSize) {
        //分页
        PageHelper.startPage(page, pageSize);
        //查询所有未支付账单
        List<Bill> billList = billMapper.serviceBill();
        Page<Bill> list = new Page<>();
        //判断是否携带条件
        if (searchMap != null && searchMap.size()>0){
            for (Bill bill : billList) {
                String storeId = bill.getStoreId().toString();
                if (storeId.equals(searchMap.get("storeId")) || bill.getStoreName().equals(searchMap.get("storeName"))){
                    //将符合条件的数据储存
                    list.add(bill);
                }
            }
            return list;
        }
        return (Page<Bill>) billList;
    }


    /**
     * 条件查询
     *
     * @param searchMap
     * @return
     */
    @Override
    public List<Bill> selectBill(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        List<Bill> bills = billMapper.selectByExample(example);
        return bills;
    }

    /**
     * 支付账单
     *
     * @param codeList
     * @return
     */
    @Override
    public Map selectCode(List<String> codeList) {
        //创建map集合封装数据
        Map<String, Object> map = new HashMap();
        //账单
        List<Bill> billList = new ArrayList<>();
        //账单金额
        Integer price = 0;
        Bill bill = new Bill();
        for (String code : codeList) {
            //根据账单编码查询账单表数据
            bill.setCode(code);
            Bill bill1 = billMapper.selectOne(bill);
            billList.add(bill1);
            //计算账单金额
            if (bill1 != null) {
                price += bill1.getPrice();
            }
        }
        map.put("price", price);
        map.put("billList", billList);

        return map;
    }

    /**
     * 取消支付账单
     * @param code
     * @param storeId
     * @return
     */
    @Override
    public Map deleteBill(String code,Integer storeId) {
        //创建map集合封装数据
        Map<String, Object> map = new HashMap();
        List<Bill> billList = billMapper.deleteBill(code,storeId);
        //账单金额
        Integer price = 0;
        for (Bill bill : billList) {
            price += bill.getPrice();
        }
        map.put("billList",billList);
        map.put("price",price);
        return map;
    }


    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Bill.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // 账单编码
            if (searchMap.get("code") != null && !"".equals(searchMap.get("code"))) {
                criteria.andEqualTo("code", searchMap.get("code"));
            }
            //商家编号
            if (searchMap.get("storeId") != null && !"".equals(searchMap.get("storeId"))) {
                criteria.andEqualTo("storeId", searchMap.get("storeId"));
            }
            // 商家名称
            if (searchMap.get("storeName") != null && !"".equals(searchMap.get("storeName"))) {
                criteria.andEqualTo("storeName", searchMap.get("storeName"));
            }
            // 账单状态
            if (searchMap.get("payState") != null && !"".equals(searchMap.get("payState"))) {
                criteria.andEqualTo("payState", searchMap.get("payState"));
            }

            // 账单日期
            if (searchMap.get("startTime") != null && !"".equals(searchMap.get("startTime")) && searchMap.get("endTime") != null && !"".equals(searchMap.get("endTime"))) {
                criteria.andBetween("createTime", searchMap.get("startTime"), searchMap.get("endTime"));
            }
        }
        return example;
    }
}
